//
//  ViewController.swift
//  PeoplePickerNavigationController
//
//  Created by tkgz on 16/5/26.
//  Copyright © 2016年 tao.520it. All rights reserved.
//

import UIKit
import AddressBookUI
import ContactsUI

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.redColor()
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let pVC = PeoplesPickerNavigationController()
        pVC.peoplesDelegate = self
        
//        let rightb = UIButton(type: .Custom)
//        rightb.setTitleColor(UIColor.greenColor(), forState: .Normal)
//        rightb.setTitle("完成", forState: .Normal)
//        
//        let leftb = UIButton(type: .Custom)
//        leftb.setTitleColor(UIColor.greenColor(), forState: .Normal)
//        leftb.setTitle("取消", forState: .Normal)
//        
//        let titleView = UIButton(type: .Custom)
//        titleView.setTitle("选取照片", forState: .Normal)
//        titleView.setTitleColor(UIColor.redColor(), forState: .Normal)
//        titleView.addTarget(self, action: #selector(ViewController.didSelectPhoto), forControlEvents: .TouchUpInside)
//        
//        pVC.rightBarButtonItem = rightb
//        pVC.leftBarButtonItem = leftb
//        pVC.titleView = titleView
//        pVC.sectionIndexColor = UIColor ( red: 0.5416, green: 1.0, blue: 0.3852, alpha: 1.0 )
        
        presentViewController(pVC, animated: true, completion: nil)
    }
    
    func didSelectPhoto() {
        print( "didSelectPhoto")
    }
}
extension ViewController: PeoplesPickerNavigationControllerDelegate {
    func peoplesNavigationController(peoplesNavigationController: PeoplesPickerNavigationController, selectedContacts: [Contact]) {
        print("didSelectedContacts\(selectedContacts.count)")
        for contact in selectedContacts {
            print(contact.contactEmails.first?.email)
        }
        
        peoplesNavigationController.dismissViewControllerAnimated(true, completion: nil)
    }
}